//#include <Windows.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <WS2tcpip.h>
#include <locale.h>

char *base64_encoder(const char *str)
{
	char abc[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
	int len_str = strlen(str);
	int len_buf = len_str / 3 * 4 + ((len_str % 3)? 4 : 0);

	char *buf = new char[len_buf + 1]; 
	int i = 0;

	memset(buf,0,len_buf+1);

	for(i;i<len_str/3;i++)
	{
		
		buf[i*4] = abc[str[i*3]>>2];
		buf[i*4 + 1] = abc[((str[i*3 + 1]&0xF0)>>4) | ((str[i*3]&3)<<4)]; 
		buf[i*4 + 2] = abc[((str[i*3 + 1]&15)<<2) | ((str[i*3 + 2]&192)>>6)];
		buf[i*4 + 3] = abc[str[i*3 + 2]&63];	
	}

	switch(len_str%3)
	{
	case 1:
		buf[i*4] = abc[str[i*3]>>2];
		buf[i*4 + 1] = abc[(str[i*3 + 1]>>4) | ((str[i*3]&3)<<4)];
		buf[i*4 + 2] = '=';
		buf[i*4 + 3] = '=';
		break;

	case 2:
		buf[i*4] = abc[str[i*3]>>2];
		buf[i*4 + 1] = abc[(str[i*3 + 1]>>4) | ((str[i*3]&3)<<4)];
		buf[i*4 + 2] = abc[((str[i*3 + 1]&15)<<2) | ((str[i*3 + 2]&192)>>6)];
		buf[i*4 + 3] = '=';
		break;
	}

	return buf;
}

int main()
{
	setlocale(LC_CTYPE,"Russian");

	WSADATA wsaData;
	int iResult  = 0;
	
	iResult = WSAStartup(MAKEWORD(2,2),&wsaData);
	if(iResult != 0)
		return 1;

	printf("WSAStartup: OK\n");

	SOCKET s;

	s = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	if(s == INVALID_SOCKET)
	{
		WSACleanup();
		return 2;
	}

	printf("socket: OK\n");

	/*sockaddr_in sa;
	sa.sin_addr.S_un.S_addr =  inet_addr("74.125.43.99");
	sa.sin_family = AF_INET;
	sa.sin_port = htons(80);*/

	addrinfo hints, *result;
	memset(&hints,0,sizeof(addrinfo));

	hints.ai_socktype =  SOCK_STREAM;
	hints.ai_protocol = IPPROTO_TCP;
	hints.ai_family = AF_INET;

	iResult  = getaddrinfo("proxy.knpz.rn.ru","8080",&hints,&result);
	if(iResult != 0)
	{
		closesocket(s);
		WSACleanup();
		return 7;
	}

	printf("getaddrinfo: OK\n");

	iResult = connect(s,result->ai_addr,result->ai_addrlen);
	if(iResult == SOCKET_ERROR)
	{
		closesocket(s);
		WSACleanup();
		return 3;
	}

	freeaddrinfo(result);

	printf("connect: OK\n");

	char *str = base64_encoder("mgburmasov:2101109");

	char buf[1024] = "GET http://book.itep.ru/1/intrnet1.gif HTTP/1.1\n"
					"Host: book.itep.ru\n"
					"Connection: keep-alive\n"
					"User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) C"
					"hrome/15.0.874.121 Safari/535.2\n"
					"Proxy-Authorization: Basic ";

	strcat(buf,str);
	strcat(buf,"\n"
				"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\n"
				"Accept-Encoding: gzip,deflate,sdch\n"
				"Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4\n"
				"Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.3\n"
				"\n");

	iResult = send(s,buf,strlen(buf),0);
	if(iResult == SOCKET_ERROR)
	{
		closesocket(s);
		WSACleanup();
		return 5;
	}

	printf("send: %s\n",buf);

	FILE *fp = fopen("d:\\page.txt","w");

	printf("recv: ");
	while(true)
	{
		memset(buf,0,101);

		iResult = recv(s,buf,100,0);
		if(iResult == SOCKET_ERROR)
		{
			closesocket(s);
			WSACleanup();
			return 6;
		}
		if(iResult < 100)
			break;

		printf("%s",buf);
		fprintf(fp,"%s",buf);
	}
	
	printf("%s\n\n",buf);
	fprintf(fp,"%s",buf);

	fclose(fp);

	iResult = shutdown(s,0);
	if(iResult == SOCKET_ERROR)
	{
		closesocket(s);
		WSACleanup();
		return 4;
	}

	printf("shutdown: OK\n");

	closesocket(s);

	printf("closesocket: OK\n");

	WSACleanup();

	printf("WSACleanup: OK\n");
	
	getch();
}